﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.Shell.Implementation
{
    using System;
    using System.Reflection;
    using System.Windows.Forms;
    using ConnectBasic.Shell.Extensibility;

    internal class DefaultToolWindow : IToolWindow
    {
        private string mInvariantName;
        private string mDisplayName;
        private Type mControlType;
        private Control mControlInstance;

        internal DefaultToolWindow(string invariantName, string displayName, Type controlType)
        {
            mInvariantName = invariantName;
            mDisplayName = displayName;
            mControlType = controlType;
        }

        public string InvariantName
        {
            get { return mInvariantName; }
        }

        public string DisplayName
        {
            get { return mDisplayName; }
        }

        public Type ControlType
        {
            get { return mControlType; }
        }

        internal Control GetOrCreateControl(IShell shell)
        {
            if (mControlInstance == null  || mControlInstance.IsDisposed)
            {
                var ctor = mControlType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new[] { typeof(IShell) }, null);
                mControlInstance = ctor.Invoke(new object[] { shell }) as Control;
            }

            return mControlInstance;
        }
    }
}
